package com.migu.contacts.main;

import static com.migu.contacts.net.Service.API;

import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.frank.libandroid.BaseActivity;
import com.frank.libandroid.CenterPopup;
import com.frank.libandroid.HttpCallback;
import com.frank.libandroid.butterknife.OnClick;
import com.lxj.xpopup.XPopup;
import com.migu.contacts.R;
import com.migu.contacts.bean.BirthdayDetailBean;
import com.migu.contacts.bean.DetailBean;
import com.migu.contacts.bean.TimerBean;
import com.migu.contacts.databinding.ActivityBirthdayCountDownBinding;
import com.migu.contacts.databinding.PopTimerDeleteBinding;
import com.migu.contacts.widget.Label2;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class BirthdayCountDownActivity extends BaseActivity<ActivityBirthdayCountDownBinding> {
    @Subscribe(sticky = true)
    public void onMessage(TimerBean bean) {
        EventBus.getDefault().removeStickyEvent(bean);

        API.getCalendarBirthdayDetail(bean.id).watch(this, new HttpCallback<DetailBean<BirthdayDetailBean>>() {
            @Override
            protected void onSuccess(DetailBean<BirthdayDetailBean> actionBeanDetailBean) {
                mDetailBean = actionBeanDetailBean;
                mBinding.setRaw(actionBeanDetailBean);
                mBinding.setBean(actionBeanDetailBean.data);
                BirthdayDetailBean data = actionBeanDetailBean.data;
                mBinding.birthday.setBean(new Label2.LabelBean("提醒生日", data.getBirthdayType(), 0, true));
                mBinding.notice.setBean(new Label2.LabelBean("提醒日期", data.getTipTime(), 0, true));
                mBinding.toggle1.setChecked(data.tipStatus);
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                super.onError(errorCode, errorMessage);
                finish();
            }
        });
    }

    DetailBean<BirthdayDetailBean> mDetailBean;

    public void delete(View view) {
        if (mDetailBean == null) {
            return;
        }
        CenterPopup bottomPopup = new CenterPopup<PopTimerDeleteBinding>(mActivity) {
            @OnClick(R.id.confirm)
            public void confirm() {
                Log.d(TAG, "confirm() called");
                API.calendarDelete(mDetailBean.id).watch(mActivity, new HttpCallback<Boolean>() {
                    @Override
                    protected void onSuccess(Boolean aBoolean) {
                        if (aBoolean) {
                            ToastUtils.showShort("删除成功");
                            dismiss();
                            finish();
                        }
                    }
                });
            }

            @OnClick(R.id.quit)
            public void quit() {
                dismiss();
            }

        };
        new XPopup.Builder(mActivity).asCustom(bottomPopup).show();
    }
}